package com.tianji.promotion.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tianji.api.cache.CategoryCache;
import com.tianji.common.exceptions.BizIllegalException;
import com.tianji.common.utils.CollUtils;
import com.tianji.common.utils.StringUtils;
import com.tianji.promotion.domain.po.CouponScope;
import com.tianji.promotion.domain.vo.CouponScopeVO;
import com.tianji.promotion.mapper.CouponScopeMapper;
import com.tianji.promotion.service.ICouponScopeService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 优惠券作用范围信息 服务实现类
 * </p>
 *
 * @author zhongweiwei
 * @since 2023-06-03
 */
@Service
@RequiredArgsConstructor
public class CouponScopeServiceImpl extends ServiceImpl<CouponScopeMapper, CouponScope> implements ICouponScopeService {
    private final CategoryCache categoryCache;

    /**
     * 根据优惠卷id查询优惠卷使用范围
     *
     * @param couponID
     * @return
     */


    @Override
    public List<CouponScopeVO> queryByCouponId(Long couponID) {
        List<CouponScope> list = lambdaQuery().eq(CouponScope::getCouponId, couponID).list();
        if (CollUtils.isEmpty(list)) {
            return CollUtils.emptyList();
        }
        //获取范围信息

        List<CouponScopeVO> scopeVOS = list.stream().map(s -> {
            CouponScopeVO scope = new CouponScopeVO();
            scope.setId(s.getId());
            String name = categoryCache.getNameByLv3Id(s.getBizId());
            if (StringUtils.isNotBlank(name)) {
                scope.setName(name);
            }
            return scope;

        }).collect(Collectors.toList());

        if (CollUtils.isEmpty(scopeVOS)) {
            throw new BizIllegalException("查询限定范围出错");
        }
        return scopeVOS;
    }
}